﻿using LicenseLib;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;

namespace LicenseDemo
{
    public class BaseModel : INotifyPropertyChanged
    {
        private long id = Guid.NewGuid().GetHashCode();
        public long ID
        {
            get { return id; }
            set { id = value; RaisePropertyChanged(); }
        }

        private long key = Guid.NewGuid().GetHashCode();
        [XmlIgnore]
        public long Key
        {
            get { return key; }
            set { key = value; RaisePropertyChanged(); }
        }

        private int order = 0;
        public int Order
        {
            get { return order; }
            set { order = value; RaisePropertyChanged(); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged([CallerMemberName] string strProperty = null)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(strProperty));
            }
        }
    }

    public class PlugConfigModel : BaseModel
    {
        private string path = string.Empty;
        public string Path
        {
            get { return path; }
            set { path = value; RaisePropertyChanged(); }
        }

        private string name = string.Empty;
        public string Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged(); }
        }

        private string windowName = string.Empty;
        public string WindowName
        {
            get { return windowName; }
            set { windowName = value; RaisePropertyChanged(); }
        }

        private int timeOut = 10;
        public int TimeOut
        {
            get { return timeOut; }
            set { timeOut = value; RaisePropertyChanged(); }
        }
    }


    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        const string ResourceKey = "&HSDV(^XZDFAS(FSDVSDFASX}{|53FAS";
        public string DynicKey = "112233445566778899";

        LicenseHelper xhpHelper = new LicenseHelper();
        LicenseConfigModel LicenseConfig = null;
        public MainWindow()
        {
            InitializeComponent();
            LicenseConfig = CreateLicenseConfig(Process.GetCurrentProcess().ProcessName, $"{Process.GetCurrentProcess().ProcessName}.Lic", DynicKey);
            SaveLicenseResource($"{System.AppDomain.CurrentDomain.BaseDirectory}system32.xhp", LicenseConfig);
            xhpHelper.Init(string.Empty, ResourceKey, string.Empty);
        }

        public void SaveLicenseResource(string path, LicenseConfigModel config)
        {
            if (File.Exists(path)) File.Delete(path);
            LicenseConfigModel item = CopyByJson(config);
            xhpHelper.SaveLicenseResource(path, item);
            config.LicensePath = item.LicensePath;
        }

        public static T CopyByJson<T>(T src) where T : class
        {
            T des = null;
            try
            {
                des = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(src));
            }
            catch (Exception ex)
            {
            }
            return des;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {


            string ActivateCode = txt_Code.Text;
            if (string.IsNullOrEmpty(ActivateCode)) return;
            LicenseHelper helper = new LicenseHelper();
            helper.Init(LicenseConfig.LicensePath, LicenseConfig.Key, LicenseConfig.IV, LicenseConfig.Type);
            string realCode = helper.StrDecrypto(ActivateCode);
            if (helper.ActivateCodeCheck(realCode, out string errorMsg))
            {
                if (helper.CreateLicense(LicenseConfig.LicensePath, realCode))
                {
                    MessageBox.Show("激活成功");
                }
                else
                    MessageBox.Show("激活失败");
            }
            else
            {
                MessageBox.Show(errorMsg, "错误");
            }
        }

        public LicenseConfigModel CreateLicenseConfig(string type, string name, string key)
        {
            return xhpHelper.CreateLicenseConfig(type, name, key);
        }

        private void Button_Click1(object sender, RoutedEventArgs e)
        {
            LicenseHelper helper = new LicenseHelper();
            string path = $"{System.AppDomain.CurrentDomain.BaseDirectory}\\system32.xhp";
            if (File.Exists(path))
            {
                string encryptoContent = File.ReadAllText(path);
                helper.Init(LicenseConfig.LicensePath, LicenseConfig.Key, LicenseConfig.IV, LicenseConfig.Type);
                string code = helper.GetSystemInfo("111");
                string mCode = helper.StrEncrypto(code);
                helper.Init(string.Empty, ResourceKey, string.Empty);
                txt_Machine.Text = helper.StrEncrypto($"{encryptoContent} {mCode}");
            }
            else
            {
                MessageBox.Show("未找到授权配置文件。");
            }
        }
    }
}
